package com.yanqu.road.server.cmd.monopolymarket.office;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/11 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, desc = "领取榷署奖励")
public class MonopolyMarketOfficeGetRewardReportCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!MonopolyMarketCommonMgr.isSystemOpen(MonopolyMarketMgr.group, eMonopolyMarketResType.OFFICE.getValue())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        // 领取奖励
        MonopolyMarketMgr.refreshUser(player,false,false);
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);

        MonopolyMarketProto.MonopolyMarketGetQueShuRewardRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketGetQueShuRewardRespMsg.newBuilder();
        if(marketModule.baseData.getTeaLastNum() > 0){
            Property reward = new Property(GameConfig.MONOPOLYMARKET_GUEST_TEA_ID, BigInteger.valueOf(marketModule.baseData.getTeaLastNum()));
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.MonopolyMarket,eLogMoneyType.MonopolyMarketOfficeTeaReward);

            marketModule.baseData.setTeaLastNum(0);
            builder.setReward(PropertyHelper.parsePropertyToString(reward));
            marketModule.synRoleData();
        }
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketGetQueShuRewardRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketGetQueShuRewardRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_OFFICE_GET_REWARD_REPORT, builder);
        }
    }

}
